package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.dto.MyStaffSearchDto;
import zhihuilan.dto.MyStaffShopDto;
import zhihuilan.dto.MyStaffShopGoodsDto;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyStaff;
import zhihuilan.entity.MyStaffSearch;
import zhihuilan.entity.MyStaffShop;
import zhihuilan.mapper.MyStaffSearchMapper;
import zhihuilan.params.query.MyStaffSearchQueryParam;
import zhihuilan.service.MyStaffSearchService;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyStaffSearchServiceImpl extends ServiceImpl<MyStaffSearchMapper, MyStaffSearch> implements MyStaffSearchService {


    @Override
    public void setSearchKeyword(String keyword, Integer member, String tbName, Integer type) {
        if (member != null && member == 0) {
            return;
        }
        QueryWrapper<MyStaffSearch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_id", member);
        queryWrapper.eq("keyword", keyword);
        queryWrapper.eq("type", type);
        MyStaffSearch myStaffSearch = baseMapper.selectOne(queryWrapper);
        if (!ObjectUtils.isEmpty(myStaffSearch)) {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", myStaffSearch.getId());
            MyStaffSearch staffSearch = new MyStaffSearch();
            staffSearch.setCreateTime(new Date().getTime() / 1000);
            staffSearch.setCreateUser(member + "");
            baseMapper.update(staffSearch, queryWrapper);
        } else {
            MyStaffSearch myStaffSearch1 = new MyStaffSearch();
            myStaffSearch1.setStaffId(member);
            myStaffSearch1.setKeyword(keyword);
            myStaffSearch1.setCreateTime(new Date().getTime() / 1000);
            myStaffSearch1.setCreateUser(member + "");
            myStaffSearch1.setTbName(tbName);
            myStaffSearch1.setType(type);
            baseMapper.insert(myStaffSearch1);
        }

    }

    @Override
    public WxMiniPageDto<MyStaffSearchDto> selectPageList(MyStaffSearchQueryParam queryParam) {
        QueryWrapper<MyStaffSearch> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(queryParam)) {
            return new WxMiniPageDto<>();
        }
        if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
            queryWrapper.eq("staff_id", queryParam.getStaffId());
        }
        if (!ObjectUtils.isEmpty(queryParam.getType())) {
            queryWrapper.eq("type", queryParam.getType());
        }
        queryWrapper.orderByDesc("create_time");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyStaffSearch> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        WxMiniPageDto<MyStaffSearchDto> result = new WxMiniPageDto(page.getCurrent(), page.getSize());
        BeanUtils.copyProperties(page, result);
        List<MyStaffSearchDto> convertList = page.getRecords().stream().map(po -> {
            MyStaffSearchDto dto = new MyStaffSearchDto();
            BeanUtils.copyProperties(po, dto);
            return dto;
        }).collect(Collectors.toList());
        result.setData(convertList);
        return result;
    }

    @Override
    public Boolean deleteById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        return this.baseMapper.deleteById(id) > 0 ? true : false;
    }

    @Override
    public Boolean deleteByStaffAll(Integer staffId) {
        if (ObjectUtils.isEmpty(staffId)) {
            return false;
        }
        QueryWrapper<MyStaffSearch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_id", staffId);
        return this.baseMapper.delete(queryWrapper) > 0 ? true : false;
    }
}
